package com.snsoft{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.*;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	[SWF(backgroundColor="#000000", frameRate="60")];
	
	/**
	 * 
	 * @author Administrator
	 * 
	 */	
	public class Main extends Sprite {
		
		//3D场景
		private var _scene :Scene3D;
		
		//摄像机
		private var _camera :Camera3D;
		
		//材质
		private var _material:MaterialObject3D;
		
		//显示贴图物体
		private var _displayObj:DisplayObject3D;
		
		private var screnX:Number = 300;
		private var screnY:Number = 200;
		private var cmrR:Number =100;
		private var sphR:Number = 1500;
		
		private var viewport:Viewport3D;
		
		private var renderer:BasicRenderEngine;
		
		private var mouseDownPlace:Point;
		
		private var isMouseDown:Boolean = false;
		
		public function Main() {
			
			stage.scaleMode = "noScale";
			
			// 创建3D舞台的容器
			viewport = new Viewport3D(0,0,true,true);
			
			this.addChild(viewport);
			
			renderer = new BasicRenderEngine();
			
			// 创建3D舞台
			_scene = new Scene3D();
			
			// 创建摄像头
			_camera = new Camera3D();
			_camera.z = 000;
			_camera.x = 000;
			_camera.y = 000;
			_camera.zoom = 40;
			
			//黑色的线框材质
			_material = new BitmapFileMaterial("7.jpg");
			_material.oneSide = false;
			_material.widthOffset = 2800;
			_material.heightOffset = 900;	
			
			_material.addEventListener(FileLoadEvent.LOAD_COMPLETE,handler);
			
			
			
		}
		
		private function handler(e:Event):void{
			trace("handler");
			//弄个球玩玩
			_displayObj = new Sphere(_material,sphR, 40, 40);
			
			//把显示对象添加到舞台
			_scene.addChild(_displayObj);
			
			//把摄像头看到的显示出来
			
			_camera.rotationX = 0;
			_camera.rotationY = 0;
			renderer.renderScene(_scene,_camera,viewport);
			
			this.addEventListener(MouseEvent.MOUSE_WHEEL, handlerMouseWheel);
			this.addEventListener(MouseEvent.MOUSE_DOWN,handlerMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,handlerMouseUp);
			this.addEventListener(Event.ENTER_FRAME,handlerEnterFrame);
		}
		
		private function handlerMouseDown(e:Event):void{
			mouseDownPlace = new Point(this.mouseX,this.mouseY);
			isMouseDown = true;
		}
		
		private function handlerMouseUp(e:Event):void{
			isMouseDown = false;
		}
		
		private function handlerEnterFrame(e:Event):void{
			if(isMouseDown){
				var currenMousePlace:Point = new Point(this.mouseX,this.mouseY);
				
				var px:Number = mouseDownPlace.x - currenMousePlace.x;
				var py:Number = mouseDownPlace.y - currenMousePlace.y;
				
				var ry:Number = _camera.rotationX - int(py/20);
				if(ry > 90){
					ry = 90;
				}
				else if(ry < -90){
					ry = -90;
				}
				_camera.rotationX = ry;
				
				var rx:Number = _camera.rotationY - int(px/20);
				if(rx > 360){
					rx -= 360;
				}
				else if(rx < -360){
					rx += 360;
				}
				_camera.rotationY = rx;
				renderer.renderScene(_scene,_camera,viewport);
			}
		}
		
		private function handlerMouseWheel(event:MouseEvent):void{
			var i:int = event.delta/3;
			_camera.zoom += i;
			if(_camera.zoom  < 4){
				_camera.zoom  =4;
			}
			renderer.renderScene(_scene,_camera,viewport);
		}
		
		 
	}
}